﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Incendios0_1.tools;
using System.Data.Linq;

namespace Incendios0_1.user_managment
{
    class UserControler
    {
        public static void AddUser(Users newUser)
        {
            DBControler.db.Users.InsertOnSubmit(newUser);
        }

        public static bool SaveChanges()
        {
            if (DBControler.SaveChanges() == null)
            {
                return true;
            }
            return false;
        }

        public static Users SearchUser(String userName)
        {
            IQueryable<Users> users = from u
                                       in DBControler.db.Users
                                       where u.Username == userName
                                       select u;
            if (users.Count() > 0)
                return users.First();
            else
                return null;
        }

        public static Users SearchUserbyUserAndPassword(String userName, String password)
        {
            IQueryable<Users> users = from u
                                       in DBControler.db.Users
                                      where u.Username == userName && u.Password == password
                                      
                                      select u;
            if (users.Count() > 0)
                return users.First();
            else
                return null;
        }

        internal static void Cancel(Users User)
        {
            DBControler.db.Refresh(RefreshMode.OverwriteCurrentValues, User);
        }

        internal static void Delete(Users user)
        {
            DBControler.db.Users.DeleteOnSubmit(user);
            DBControler.db.SubmitChanges();
        }
    }
}
